package middleman;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.Socket;

import messages.Message;

public class MalloryThread extends Thread
{
    Mallory mallory;

    Socket socket;

    ObjectInputStream in;

    private int threadId;

    public MalloryThread( Mallory mallory, Socket socket, int threadId )
            throws IOException {
        this.mallory = mallory;
        this.socket = socket;
        this.threadId = threadId;
        in = new ObjectInputStream( socket.getInputStream() );
    }

    public void run() {
        Message msg;
        try {
            while( (msg = (Message) in.readObject()) != null ) {
                msg = mallory.changeMessage( msg );
                mallory.forwardMessage( msg, threadId );
            }
        } catch( IOException e ) {
            e.printStackTrace();
        } catch( ClassNotFoundException e ) {
            e.printStackTrace();
        }
    }

    public int getThreadId() {
        return threadId;
    }
}
